package com.example.test.dialogfactory;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

public class MainActivity63 extends AppCompatActivity {

    private ViewPager2 mViewPager;

    public static void start( Context context ) {

        Intent starter = new Intent( context, MainActivity63.class );
        context.startActivity( starter );
    }

    @Override
    protected void onCreate( Bundle savedInstanceState ) {

        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main63 );
        initView();
    }

    private void initView() {

        mViewPager = (ViewPager2) findViewById( R.id.viewPager );
        mViewPager.setAdapter( new Adapter() );
    }

    private class Adapter extends FragmentStateAdapter {

        public Adapter() {

            super( MainActivity63.this );
        }

        @NonNull
        @Override
        public Fragment createFragment( int position ) {

            if ( position == 1 ) {
                return new PageStyle2();
            }
            return Page.newInstance( "页面" + position );
        }

        @Override
        public int getItemCount() {

            return 3;
        }
    }

    public static class Page extends Fragment {

        private String mText1;

        public static Page newInstance( String text ) {

            Bundle args = new Bundle();
            args.putString( "text", text );

            Page fragment = new Page();
            fragment.setArguments( args );
            return fragment;
        }

        @Override
        public void onCreate( @Nullable Bundle savedInstanceState ) {

            super.onCreate( savedInstanceState );
            if ( getArguments() != null ) {
                mText1 = getArguments().getString( "text" );
            }
        }

        @Nullable
        @Override
        public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState ) {

            return inflater.inflate( R.layout.activity_main63_item, container, false );
        }

        @Override
        public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {

            super.onViewCreated( view, savedInstanceState );
            initView( view );
        }

        private void initView( @NonNull View view ) {

            TextView text = (TextView) view.findViewById( R.id.text );
            text.setText( mText1 );
        }
    }

    public static class PageStyle2 extends Fragment {

        private ViewPager2 mViewPager;

        @Nullable
        @Override
        public View onCreateView( @NonNull LayoutInflater inflater, @Nullable ViewGroup container,
            @Nullable Bundle savedInstanceState ) {

            return inflater.inflate( R.layout.activity_main63_item_2, container, false );
        }

        @Override
        public void onViewCreated( @NonNull View view, @Nullable Bundle savedInstanceState ) {

            super.onViewCreated( view, savedInstanceState );
            initView( view );
        }

        private void initView( View view ) {

            mViewPager = (ViewPager2) view.findViewById( R.id.viewPager );
            mViewPager.setAdapter( new Adapter() );
        }

        private class Adapter extends FragmentStateAdapter {

            public Adapter() {

                super( PageStyle2.this );
            }

            @NonNull
            @Override
            public Fragment createFragment( int position ) {

                return Page.newInstance( "子页面" + position );
            }

            @Override
            public int getItemCount() {

                return 3;
            }
        }
    }
}